package com.ibeeking.found.msg.rest.service.impl;


import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.msg.rest.service.IMpMiniprogramService;
import com.ibeeking.found.msg.service.common.dos.MpMiniprogramDO;
import com.ibeeking.found.msg.service.common.param.MpMiniprogramParam;
import com.ibeeking.found.msg.service.common.query.MpMiniprogramPageQuery;
import com.ibeeking.found.msg.service.common.query.MpMiniprogramQuery;
import com.ibeeking.found.msg.service.common.vo.MpMiniprogramVO;
import com.ibeeking.found.msg.service.mapper.MpMiniprogramMapper;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName MpMiniprogramServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-02 20:18
 **/
@Service
public class MpMiniprogramServiceImpl extends ServiceImpl<MpMiniprogramMapper, MpMiniprogramDO> implements IMpMiniprogramService {

    @Resource
    private MpMiniprogramMapper mpMiniprogramMapper;

    @Override
    public List<MpMiniprogramVO> list(MpMiniprogramQuery mpMiniprogramQuery) {
        List<MpMiniprogramDO> mpMiniprogramDOS = mpMiniprogramMapper.selectList(new LambdaQueryWrapper<MpMiniprogramDO>()
                .eq(MpMiniprogramDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue()));
        return JsonUtils.jsonToList(mpMiniprogramDOS, MpMiniprogramVO.class);
    }

    @Override
    public Page<MpMiniprogramVO> pageList(MpMiniprogramPageQuery mpMiniprogramPageQuery) {
        Page<MpMiniprogramDO> page = new Page<>(mpMiniprogramPageQuery.getPageNum(), mpMiniprogramPageQuery.getPageSize());
        QueryWrapper<MpMiniprogramDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotBlank(mpMiniprogramPageQuery.getName()), MpMiniprogramDO::getName, mpMiniprogramPageQuery.getName())
                .orderByDesc(MpMiniprogramDO::getCreateTime);
        page = mpMiniprogramMapper.selectPage(page, queryWrapper);
        return BeanUtil.convertPage(page, MpMiniprogramVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MpMiniprogramParam mpMiniprogramParam) {
        MpMiniprogramDO mpMiniprogramDO = BeanUtil.convertBean(mpMiniprogramParam, MpMiniprogramDO.class);
        mpMiniprogramDO.setCreateTime(LocalDateTime.now());
        mpMiniprogramDO.setModifyTime(LocalDateTime.now());
        return mpMiniprogramMapper.insert(mpMiniprogramDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(MpMiniprogramParam mpMiniprogramParam) {
        MpMiniprogramDO mpMiniprogramDO = BeanUtil.convertBean(mpMiniprogramParam, MpMiniprogramDO.class);
        mpMiniprogramDO.setModifyTime(LocalDateTime.now());
        return mpMiniprogramMapper.updateById(mpMiniprogramDO) > 0;
    }

    @Override
    public MpMiniprogramVO view(Long id) {
        MpMiniprogramDO mpMiniprogramDO = mpMiniprogramMapper.selectById(id);
        return BeanUtil.convertBean(mpMiniprogramDO, MpMiniprogramVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(Long id, Integer status) {
        MpMiniprogramDO mpMiniprogramDO = new MpMiniprogramDO();
        mpMiniprogramDO.setId(id);
        mpMiniprogramDO.setPublishStatus(status);
        mpMiniprogramDO.setModifyTime(LocalDateTime.now());
        return mpMiniprogramMapper.updateById(mpMiniprogramDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        return mpMiniprogramMapper.deleteById(id) > 0;
    }
}
